package sicnu.cs.ich.security.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.springframework.security.core.userdetails.UserDetails;
import sicnu.cs.ich.api.common.annotations.transaction.EncryptTransaction;
import sicnu.cs.ich.api.common.annotations.transaction.SensitiveData;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author Lenovo
 */
@With
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@SensitiveData
@TableName(value = "t_user", resultMap = "userResultMap")
public class User implements UserDetails, Serializable {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String username;
    private String openId;
    @JsonIgnore
    private String password;
    @EncryptTransaction
    private String email;
    @EncryptTransaction
    private String mobile;
    private Date createTime;
    @JsonIgnore
    private Date expireTime;
    @Builder.Default
    @JsonIgnore
    private Boolean status = true;
    @Builder.Default
    @JsonIgnore
    @TableField(exist = false)
    private Boolean accountNonExpired = true;
    @Builder.Default
    @JsonIgnore
    @TableField(exist = false)
    private Boolean accountNonLocked = true;
    @Builder.Default
    @TableField(exist = false)
    @JsonIgnore
    private Boolean credentialsNonExpired = true;
    @Builder.Default
    @TableField(exist = false)
    @JsonIgnore
    private Boolean enabled = true;
    private List<Role> roles;
    @TableField(exist = false)
    private List<Permission> authorities;

    @Override
    public List<Permission> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
